package com.example.apitest;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class BookObject extends BaiHuiJSONObject{
	private static final String TAG = "BookObject";	
	private static final String Cmd_Add = "http://creator.baihui.com/api/test/json/Cmd_AppName/Cmd_Object/add/apikey=Cmd_ApiKey&ticket=Cmd_Ticket";
	private static final String Cmd_View = "http://creator.baihui.com/api/json/Cmd_AppName/view/Cmd_Object/apikey=Cmd_ApiKey&ticket=Cmd_Ticket";	
	private static final String Cmd_Update = "http://creator.baihui.com/api/json/Cmd_AppName/Cmd_Object/update/apikey=Cmd_ApiKey&ticket=Cmd_Ticket";
	private static final String Cmd_Delete = "http://creator.baihui.com/api/json/Cmd_AppName/Cmd_Object/delete/apikey=Cmd_ApiKey&ticket=Cmd_Ticket";	
	
	private static final String Cmd_AppName = "pongo-app-test";
	private static final String Object_Form = "Book_From";	
	private static final String Object_View = "Book_View";	
	
	private static final String Tag_ISBN = "isbn13";
	private static final String Tag_Origin_Title = "origin_title";		
	private static final String Tag_Title = "title";
	private static final String Tag_Image = "image";
	
	public BookObject(JSONObject object) throws JSONException {
		super(object);
	}	
	
	public BookObject() {
		super();
	}

	public String ConstructCMD(String Cmd_String, String Object, String Apikey, String Ticket){
		String cmdstring = null;
		
		cmdstring = Cmd_String.replaceAll("Cmd_AppName", Cmd_AppName);
		cmdstring = cmdstring.replaceAll("Cmd_Object", Object);		
		cmdstring = cmdstring.replaceAll("Cmd_ApiKey", Apikey);
		cmdstring = cmdstring.replaceAll("Cmd_Ticket", Ticket);			
		Log.i(TAG,"ConstructCMD="+cmdstring);		
		return cmdstring;
	}
	
	
	@Override
	protected long GenerateRecordId(){
		long id = 0;
		
		return id;
	}
	
	
	@Override
	public void Add(Command Cmd, String Apikey, String Ticket) {
		BookObject bookobject = this;
		JSONObject resultobject = null;
		try {
			String cmdstring = ConstructCMD(Cmd_Add, Object_Form, Apikey, Ticket);
						
/*
				String title = new String(bookobject.getString("title").getBytes("ISO-8859-1"), "UTF-8");
				String Cmd_Parmeter = "&ISBN="+URLEncoder.encode(bookobject.getString("isbn13"),"utf8")+"&BookName="+URLEncoder.encode(bookobject.getString("origin_title"),"utf8");
				
				resultobject = Cmd.DoCommand(cmdstring+Cmd_Parmeter);				
*/

			    List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>(); 
			    params.add(new BasicNameValuePair("BookID", getUUIDHigh())); 			    
			    params.add(new BasicNameValuePair("ISBN", bookobject.getString(Tag_ISBN))); 				
			    params.add(new BasicNameValuePair("BookName", bookobject.getString(Tag_Title)));
			    params.add(new BasicNameValuePair("Image", bookobject.getString(Tag_Image)));
			    
				try {
					resultobject = Cmd.DoPostCommand(cmdstring, new UrlEncodedFormEntity(params, HTTP.UTF_8));
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}				

		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
	}
	
	@Override
	public void Update(Command Cmd, String Apikey, String Ticket) {
		BookObject bookobject = this;
		JSONObject resultobject = null;
		String cmdstring = ConstructCMD(Cmd_Update, Object_Form, Apikey, Ticket);
			
	    List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();  
	    params.add(new BasicNameValuePair("criteria", "ISBN=9787535463180"));
	    params.add(new BasicNameValuePair("BookID", "1"));
	    
		try {
			resultobject = Cmd.DoPostCommand(cmdstring, new UrlEncodedFormEntity(params, HTTP.UTF_8));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Override
	public void Delete(Command Cmd, String Apikey, String Ticket) {
		BookObject bookobject = this;
		JSONObject resultobject = null;
		String cmdstring = ConstructCMD(Cmd_Delete, Object_Form, Apikey, Ticket);
			
	    List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();  
	    params.add(new BasicNameValuePair("criteria", "ISBN=9787506355957"));

		try {
			HttpEntity entity = new UrlEncodedFormEntity(params, HTTP.UTF_8);
			resultobject = Cmd.DoPostCommand(cmdstring, entity);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Override
	public JSONObject View(Command Cmd, String Apikey, String Ticket) {
		BookObject bookobject = this;
		String result = null;
		JSONObject resultobject = null;
		String cmdstring = ConstructCMD(Cmd_View, Object_View, Apikey, Ticket);
		result = Cmd.DoGetCommandString(cmdstring);

		int index = result.indexOf("{");
		result = result.substring(index);
		try {
			resultobject = new JSONObject(result);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return resultobject;
	}	
}
